package com.drodds.game;

import java.util.ArrayList;
import android.graphics.Rect;
import android.content.res.Resources;
import android.graphics.Canvas;

public class SlotMachine {
	private static final float spinTime = 3.0f;
	private static final float staggerTime = 0.2f;
	private static final int hReelPadding = 2;
	private static final int vReelPadding = 2;
	
	private int numReels;
	private int numStops;
	private int numValues;
	private int result = 0;
	
	private ArrayList<Reel> arrReels = new ArrayList<Reel>();
	
	public boolean spinning;
	public boolean resultReady = false;
	private boolean firstTime = true;
	private float elapsedSpinTime = 0.0f;
	
	public SlotMachine(int reels, int stops, int values){
		numReels = reels;
		numStops = stops;
		numValues = values;
	}
	
	public void Init(Resources res) {
		for (int i = 0; i < numReels; i++) {
			arrReels.add(new Reel(res, numStops, numValues));
			arrReels.get(i).reelNum = i;
		}
	}
	
	public void draw(Canvas c, Rect boundsRect) {
		int startX = boundsRect.left + hReelPadding;
		int startY = boundsRect.top + vReelPadding;
		int reelWindowSize = (boundsRect.width() - hReelPadding * (arrReels.size() + 1)) / arrReels.size();
		
		for (Reel r : arrReels) {
			Rect bounds = new Rect(startX, startY, startX + reelWindowSize, startY + reelWindowSize);
			r.draw(c, bounds);
			
			startX += reelWindowSize + hReelPadding;
		}
	}
	
	public void update(float deltaTime) {
		
		if (!spinning) {
			CalcResult();
			return;
		}
		
		if (firstTime) {
			for (Reel r : arrReels)
				r.Spin();
			
			firstTime = false;
		}
		else {
			for (int i = 0; i < arrReels.size(); i++) {
				if ((elapsedSpinTime > (staggerTime * i)) && !arrReels.get(i).stopped)
					arrReels.get(i).update(deltaTime);
			}
		}
		
		int stopCounter = 0;
		for (int i = 0; i < arrReels.size(); i++) {
			if (elapsedSpinTime > (spinTime + staggerTime * i)) {
				if (!arrReels.get(i).stopped) {
					arrReels.get(i).Stop();
				}
				else
					stopCounter++;
					
			}
		}
		
		if (stopCounter >= arrReels.size()) {
			spinning = false;
			elapsedSpinTime = 0.0f;
		}

		elapsedSpinTime += deltaTime;
	}
	
	private void CalcResult() {
		int currResult = -1;
		int numMatches = 0;

		for (Reel r : arrReels) {
			if (currResult == -1) {
				currResult = r.GetLastValue();
				numMatches++;
			}
			else if (currResult != r.GetLastValue())
				break;
			else 
				numMatches++;
		}

		result = getExponent(currResult, numMatches);
		resultReady = true;
	}
	
	public void Spin() {
		if (spinning)
			return;
		else {
			spinning = true;
			elapsedSpinTime = 0.0f;
			firstTime = true;
			resultReady = false;
		}
	}
	
	public int GetResult() {
		return result;
	}
	
	public int GetResultRange() {
		int rtnVal = 0;
		
		for (AllValues v : AllValues.values()) {
			if (v.GetVal() > rtnVal)
				rtnVal = v.GetVal();
		}
		
		return getExponent(rtnVal, arrReels.size());
	}
	
	public int getExponent(int a, int b) {
		if (b == 1)
			return a;
		
		int x = a*getExponent(a, b-1);
		return x;
	}
}
